Add keepalive to responseInputStream timeout scheduler executor to ma…#6756
Add keepalive to responseInputStream timeout scheduler executor to ma…#6756
Conversation
…ke sure scheduler thread doenst leak
| static final ScheduledExecutorService INSTANCE; | ||
|
|
||
| static { | ||
| ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, r -> { |
There was a problem hiding this comment.
nit, we can probably just do INSTANCE = new ScheduledThreadPoolExecutor(...?
There was a problem hiding this comment.
I don't know if we can do this.
setKeepAliveTime and allowCoreThreadTimeOut are methods on ScheduledThreadPoolExecutor (concrete class), not on ScheduledExecutorService (interface).
Since INSTANCE is the interface, we need a local variable of the concrete type to call those methods
before assigning to INSTANCE.
core/sdk-core/src/main/java/software/amazon/awssdk/core/ResponseInputStream.java
Show resolved
Hide resolved
| t.setDaemon(true); | ||
| return t; | ||
| }); | ||
| executor.setKeepAliveTime(DEFAULT_TIMEOUT.getSeconds(), TimeUnit.SECONDS); |
There was a problem hiding this comment.
Suggestion: DEFAULT_TIMEOUT is the stream read timeout (how long before aborting an unread stream). The keep-alive is how long an idle thread survives. These are conceptually different values that happen to be 60s today. If someone changes DEFAULT_TIMEOUT in the future, the thread keep-alive silently changes too. Consider a separate constant
There was a problem hiding this comment.
Good call out, I'll add a separate constant.
Since currently it uses DEFAULT_TIMEOUT What do you think about the 60s timeout of the scheduler thread? Do we want to make it shorter? ie; 15 seconds?
|



Addresses: #6567
Background and context
ResponseInputStreamuses a staticScheduledExecutorServiceto abort streams that aren't read within 60 seconds (to prevent connection leaks). The executor's thread never terminates because core threads are kept alive indefinitely by default. This causes theresponse-input-stream-timeout-schedulerthread to persist for the lifetime of the JVM, even when no streams exist.To fix this, we can set
allowCoreThreadTimeOut(true)and a 60s keep-alive on the executor. The thread now dies after 60 seconds of idleness and respawns on demand (matchesTransferManagerConfiguration.java).Testing
Since there is a timeout and a keep alive mechanism it's hard to write a deterministic test to prove the fix works. Additionally, adding a test with a sleep method of >1min is not feasible.
The following test fails on Master, and passes after the code change introduced in the PR: